<template>
  <div>
    <el-card class="swiper-container">
      <template #header>
        <div class="header">
          <el-button type="primary" @click="handleAdd">增加</el-button>
          <!--  <el-popconfirm title="确定删除吗？" @confirm="handleDelete">
            <template #reference>
              <el-button type="danger" size="small" icon="el-icon-delete"
                >批量删除</el-button
              >
            </template>
          </el-popconfirm> -->
        </div>
      </template>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="55"> </el-table-column>
        <el-table-column
          prop="notice_id"
          label="排序值"
          width="120"
        ></el-table-column>
        <el-table-column prop="text" label="文本" width="500">
        </el-table-column>
        <el-table-column label="是否展示" width="150">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isNotice"
              @change="changeShow(scope.row.isNotice, scope.row.notice_id)"
            />
          </template>
        </el-table-column>

        <el-table-column
          prop="atime"
          label="添加时间"
          width="200"
        ></el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              @click="openElMessageBox(scope.row.notice_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--总数超过一页，再展示分页器-->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="changePage"
      />
    </el-card>
    <!-- :reload="getCarousels" -->
    <DialogNotice ref="addBanner" :type="type" @changeTableData="changeData" />
  </div>
</template>

<script setup>
import DialogNotice from "../components/DialogNotice.vue";
import { onMounted, reactive, ref, toRefs, getCurrentInstance } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { timeTrans } from "../../utils";
import { getNotice, deleteNotice, showNotice } from "../../api/notice";

// 在setup中拿到this，，proxy相当于this
const { proxy } = getCurrentInstance();
const multipleTable = ref(null);
const addBanner = ref();
const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 10, // 分页大小
  type: "add", // 操作类型
});

let {
  loading,
  tableData,
  multipleSelection,
  total,
  currentPage,
  pageSize,
  type,
} = toRefs(state);

onMounted(async () => {
  // 获取轮播图列表
  const notice = await getNotice(proxy);
  notice.forEach((item) => {
    // 处理是否展示图片
    if (item.isNotice === 1) {
      item.isNotice = true;
    } else {
      item.isNotice = false;
    }
    // 处理时间
    item.atime = timeTrans(item.atime);
  });
  state.tableData = notice;
});
// 子传父
const changeData = (data) => {
  data.forEach((item) => {
    // 处理是否展示广告
    if (item.isNotice === 1) {
      item.isNotice = true;
    } else {
      item.isNotice = false;
    }
    // 处理时间
    item.atime = timeTrans(item.atime);
  });
  state.tableData = data;
};

// 是否展示图片
const changeShow = (isNotice, notice_id) => {
  console.log("触发", isNotice, notice_id);
  showNotice(proxy, { isNotice, notice_id }).then((data) => {
    if (data.code === 200) {
      // 提示
      ElMessage({
        type: "success",
        message: "更改成功",
      });
    }
  });
};

// 添加轮播图片
const handleAdd = () => {
  state.type = "add";
  addBanner.value.open();
};
// 修改轮播图片
const handleEdit = (notice) => {
  state.type = "edit";
  addBanner.value.open(notice);
};
// 选择项
const handleSelectionChange = (val) => {
  state.multipleSelection = val;
};
/* // 批量删除
const handleDelete = () => {
  if (!state.multipleSelection.length) {
    ElMessage.error("请选择项");
    return;
  }
}; */

const changePage = (val) => {
  state.currentPage = val;
  // getCarousels();
};

// 点击删除弹窗
const openElMessageBox = (notice_id) => {
  ElMessageBox.confirm("是否删除当前这条广告,删呢还是删呢?", "删除", {
    confirmButtonText: "删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 发送请求删除
      // console.log(id);
      deleteNotice(proxy, { notice_id }).then((data) => {
        // console.log(data);
        if (data.code === 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
        }
        // 更新数据，再发一次请求banner
        getNotice(proxy).then((data) => {
          data.forEach((item) => {
            // 处理是否展示图片
            if (item.isNotice === 1) {
              item.isNotice = true;
            } else {
              item.isNotice = false;
            }
            // 处理时间
            item.atime = timeTrans(item.atime);
          });
          state.tableData = data;
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除失败",
      });
    });
};
</script>

<style scoped>
.swiper-container {
  min-height: 100%;
}
.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>
